package com.substation.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bean.LoginInfo;
import com.greenentity.DataDictionary;
import com.greenentity.Eqpt;
import com.greenentity.Substation;
import com.net.SubstationNet;
import com.net.UserNet;
import com.substation.R;
import com.substation.greendao.DaoSession;
import com.substation.utils.GreenDaoManager;
import com.substation.utils.SPUtils;
import com.substation.utils.T;
import com.substation.utils.TestingUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.password)
    EditText password;
    DaoSession daoSession;
    @BindView(R.id.spinner1)
    Spinner spinner;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        username.setText((String) SPUtils.get(context, "username", ""));
        password.setText((String) SPUtils.get(context, "password", ""));
        int netCount = (int) SPUtils.get(context, "netCount", 0);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                SPUtils.put(context, "netCount", i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        spinner.setSelection(netCount);
        if (LoginInfo.getInfo(context) != null) {
            startActivity(new Intent(context, MainActivity.class));
            finish();
        }
    }

    @Override
    public void success(int i, String obj) {
        switch (i) {
            case 1:
                LoginInfo loginInfo = JSON.parseObject(obj, LoginInfo.class);
                SPUtils.put(context, "token", loginInfo.getToken());
                LoginInfo.save(context, loginInfo);
                SubstationNet.getSubtation(context, this);
                break;
            case 2:
                JSONObject object = JSON.parseObject(obj);
                String eqptContent = object.getString("restGsubseqptList");
                String substationContent = object.getString("restWsubstationList");
                String dictionarieContent = object.getString("dictionaries");
                List<Eqpt> eqpts = JSON.parseArray(eqptContent, Eqpt.class);
                List<Substation> substations = JSON.parseArray(substationContent, Substation.class);
                List<DataDictionary> dataDictionaries = JSON.parseArray(dictionarieContent, DataDictionary.class);
                GreenDaoManager.getInstance().getmDaoSession().getSubstationDao().deleteAll();
                GreenDaoManager.getInstance().getmDaoSession().getSubstationDao().insertInTx(substations);
                GreenDaoManager.getInstance().getmDaoSession().getEqptDao().deleteAll();
                GreenDaoManager.getInstance().getmDaoSession().getEqptDao().insertInTx(eqpts);
                GreenDaoManager.getInstance().getmDaoSession().getDataDictionaryDao().deleteAll();
                GreenDaoManager.getInstance().getmDaoSession().getDataDictionaryDao().insertInTx(dataDictionaries);
                startActivity(new Intent(context, MainActivity.class));
                finish();
                break;
        }
    }

    @OnClick(R.id.login)
    public void onViewClicked() {
        PermissionGen.with(this).addRequestCode(1).permissions(Manifest.permission.READ_PHONE_STATE).request();
    }

    @PermissionSuccess(requestCode = 1)
    public void requestPhotoSuccess() {
        String name = username.getText().toString();
        String pwd = password.getText().toString();
        if (!TestingUtils.isEmpty(context, name, "用户名")) return;
        if (!TestingUtils.isEmpty(context, name, "密码")) return;
        UserNet.login(context, name, pwd, this);
    }

    @PermissionFail(requestCode = 1)
    public void requestPhotoFail() {
        T.showLong(context, "您未通过读取手机信息权限请求");
    }

}
